package cs2340.todolist.model.sqlite_database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * create a the database for todoitems
 * @author BaoTam
 *
 */
public class ToDoItemSQLiteHelper extends SQLiteOpenHelper {

	public static final String TABLE_ITEMS 			= "items";
	public static final String COLUMN_ID			= "id";
	public static final String COLUMN_USER_ID		= "user_id";
	public static final String COLUMN_TITLE			= "title";
	public static final String COLUMN_TYPE			= "type";
	public static final String COLUMN_DUE_DATE		= "due_date";
	public static final String COLUMN_RECUR_TYPE	= "recur_type";
	public static final String COLUMN_LOCATION_ADDR	= "location_addr";
	public static final String COLUMN_LOCATION_NAME = "location_name";
	public static final String COLUMN_DESCRIPTION	= "description";
	public static final String COLUMN_COMPLETED		= "completed";


	private static final String DATABASE_NAME = "todoitems.db";
	private static final int DATABASE_VERSION = 1;

	// Database creation sql statement
	private static final String DATABASE_CREATE = "create table "
			+ TABLE_ITEMS 
				+ "( " 
				+ COLUMN_ID 				+ " integer primary key autoincrement, " 
				+ COLUMN_USER_ID 			+ " integer,"
				+ COLUMN_TITLE 				+ " text not null,"
				+ COLUMN_TYPE				+ " integer,"
				+ COLUMN_DUE_DATE			+ " text,"
				+ COLUMN_RECUR_TYPE			+ " integer,"
				+ COLUMN_LOCATION_ADDR		+ " text,"
				+ COLUMN_LOCATION_NAME		+ " text,"
				+ COLUMN_DESCRIPTION		+ " text,"
				+ COLUMN_COMPLETED 			+ " text not null"
				+ ");";
	
	/**
	 * 
	 * @return an array holding the column names
	 */
	public static String[] getAllColumnNames() {
		String[] columnNames = {
				COLUMN_ID,
				COLUMN_USER_ID,
				COLUMN_TITLE,
				COLUMN_TYPE,
				COLUMN_DUE_DATE,
				COLUMN_RECUR_TYPE,
				COLUMN_LOCATION_ADDR,
				COLUMN_LOCATION_NAME,
				COLUMN_DESCRIPTION,
				COLUMN_COMPLETED
				};
		return columnNames;
	}
	
	/**
	 * instantiate the helper
	 * @param context
	 */
	public ToDoItemSQLiteHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	/**
	 * create a database if it has not been created
	 */
	public void onCreate(SQLiteDatabase database) {
		database.execSQL(DATABASE_CREATE);
	}
	
	

	/**
	 * upgrade the table
	 */
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(UserAccountSQLiteHelper.class.getName(),
				"Upgrading database from version " + oldVersion + " to "
						+ newVersion + ", which will destroy all old data");
		db.execSQL("DROP TABLE IF EXISTS" + TABLE_ITEMS);
		onCreate(db);
	}

}
